// ---------------------------------------------------------------------
//
// -- 加载场景中介层
// -- author £
//
// ---------------------------------------------------------------------

const CommonInfo = require('CommonInfo'),
    AppInfo = require('AppInfo'),
    Platform = require('Platform'),
    CommonUtil = require('CommonUtil'),
    superClass = require('mybase').BaseMediator;

let cl = cc.Class({
    name: "MainMediator",
    extends: superClass,
    properties: {
    },

    initCtor: function () {
        CommonUtil.mysuper(superClass, this, 'initCtor');
        require('Facade').startup();
        this.model = require('MainModel');
        this._lockManager = new (require('common').LockManager)();
        this._returnLevel = 0;
        this._bestTabIndex = -1;
        this._historyBestGeted = false;
        this._worldPageManager = new (require('common').PageListManager)();
        this._historyBestPManager = new (require('common').PageListManager)();
    },

    clearData: function () {
        CommonUtil.mysuper(superClass, this, 'clearData');
    },

    onRegister: function (v) {
        CommonUtil.mysuper(superClass, this, 'onRegister', v);
        this._worldPageManager.init(require('UIManager').getRankItemCount());
        this._historyBestPManager.init(require('UIManager').getRankItemCount());
    },

    onRelease: function () {
        CommonUtil.mysuper(superClass, this, 'onRelease');
        this._lockManager.clear();
    },

    main_toLobbyScene: function () {
        this._lockManager.clear();
        this.scene.toLobbyScene();
    },

    main_toGameScene: function () {
        this._lockManager.clear();
        this.scene.toGameScene();
    },

    main_inLoadData: function (value, taskName, info, state) {
        value ?
            this._lockManager.start(taskName) :
            this._lockManager.end(taskName);

        let isFree = this._lockManager.isFree();

        if (this.scene) {
            !isFree ?
                this.scene.showLoadData(info, state) :
                this.scene.hideLoadData();
        }
    },

    main_getWorldRank: function (xhrData) {
        if (xhrData.code) return;

        let list = xhrData.data,
            rank = -1, gap = -1, len = list.length;

        // if (len) {
        //     for (let i = 0; i < 99; ++i) {
        //         list[list.length] = {
        //             nick_name: list[0].nick_name + i,
        //             today_best_score: list[0].today_best_score - i - 1,
        //             avatar: list[0].avatar
        //         }
        //     }
        // }

        CommonUtil.sortRank(xhrData.data, 'today_best_score');

        for (let i = 0; i < len; ++i) {
            if (list[i].today_best_score === CommonInfo.dayBestScore) {
                rank = list[i].rank;
                break;
            }
        }

        if (len > 0) {
            gap = list[list.length - 1].today_best_score - CommonInfo.dayBestScore
        }

        this.scene.setWorldRankData({
            list: this._worldPageManager.setData(list),
            rank: rank,
            gap: gap
        });
    },

    getHistroyBest: function (xhrData) {
        if (xhrData.code) return;

        let list = xhrData.data,
            rank = -1, gap = -1, len = list.length;

        // for (let i = 0; i < 100; ++i) {
        //     list[list.length] = {
        //         nick_name: 'zazisb' + i,
        //         history_best_score: 1999 - i,
        //         avatar: 'http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83epIEMXQP0ctfqs7qshBtzsdaicLuyRnKjU8MNuceRfb0m1JnzXYf7FttroOibQVPIa74vsAVyhHwLdA/132',
        //         user_id: i,
        //         date_time: 1530115200 - i * 86400
        //     }
        // }

        this._historyBestGeted = true;
        this.scene.setBestHistory(this._historyBestPManager.setData(list));
    },

    main_groupRankHandler: function (data) {
        this.scene.showGroupRankPanel(data);
    },

    main_friendRankHandler: function (data) {
        this.scene.showFriendRankPanel(data);
    },

    main_hideRankHandler: function (data) {
        Platform.postMessage({ message: 'hide' });
        this.scene.hideRankPanel();
    },

    main_hideWorldHandler: function () {
        Platform.postMessage({ message: 'hide' });
        this.scene.hideRankPanel();
        Platform.showBannerAd();
    },

    main_worldRankHandler: function () {
        this.scene.showWorldRankPanel();
    },

    main_preWorldRankHandler: function () {
        let list = this._worldPageManager.pre();

        if (!list) return;

        this.scene.setWorldRankListData(list);
    },

    main_nextWorldRankHandler: function () {
        let list = this._worldPageManager.next();

        if (!list) return;

        this.scene.setWorldRankListData(list);
    },

    worldHistoryHandler: function () {
        this.scene.showWorldHistory();
        this.historyBestHandler();
    },

    historyBestHandler: function () {
        this._bestTabIndex = 0;

        if (this._historyBestGeted) {
            this.scene.setBestHistory(this._historyBestPManager.first());
        } else {
            require('GameConfig').userProto.queryHistoryBest(
                (this.getHistroyBest).bind(this)
            );
        }
    },

    myBestHandler: function () {
        this._bestTabIndex = 1;
        this.scene.setMyBest(CommonInfo.getMyBest());
    },

    main_worldHistoryPreHandler: function () {
        let list;

        switch (this._bestTabIndex) {
            case 0:
                list = this._historyBestPManager.pre();

                if (!list) return;

                this.scene.setBestHistory(list);
                break;
        }
    },

    main_worldHistoryNextHandler: function () {
        let list;

        switch (this._bestTabIndex) {
            case 0:
                list = this._historyBestPManager.next();

                if (!list) return;

                this.scene.setBestHistory(list);
                break;
        }
    },

    /** 是否正在数据加载或通信 */
    isFree: function () {
        return this._lockManager.isFree() && CommonInfo.sceneState === 0;
    },

    //------------------------------------------------------------------------------

    //------------------------------------------------------------------------------

    preFriendRankHandler: function () {
        Platform.postMessage({ message: 'pre_friend' });
    },

    nextFriendRankHandler: function () {
        Platform.postMessage({ message: 'next_friend' });
    },

    preGroupRankHandler: function () {
        Platform.postMessage({ message: 'pre_group' });
    },

    nextGroupRankHandler: function () {
        Platform.postMessage({ message: 'next_group' });
    },


});

let MainMediator = new cl();

module.exports = MainMediator;
